package com.qifenqian.turbot.module.ui.marketing

import android.content.Context
import android.content.Intent
import android.view.View
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant
import com.qifenqian.turbot.module.contract.MarketingContract
import com.qifenqian.turbot.module.entity.response.marketing.EarlyPayDiscountEntity
import com.qifenqian.turbot.module.presenter.MarketingPresenter
import com.qifenqian.turbot.util.SpUtils
import kotlinx.android.synthetic.main.activity_early_pay_discount.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2019/1/4 15:29
 * @description 提前买单折扣
 * @see
 * @since
 */
class EarlyPayDiscountActivity : BaseActivity<MarketingPresenter, MarketingContract.View>(), MarketingContract.View {

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, EarlyPayDiscountActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int = R.layout.activity_early_pay_discount
    override fun bindPresenter(): MarketingPresenter? = MarketingPresenter(this, this)
    private var mEarlyPayDiscountEntity: EarlyPayDiscountEntity? = null
    override fun initial() {
        mPresenter?.getEarlyPayDiscount()
        val earlyPayDiscountRequest = EarlyPayDiscountEntity(-1, SpUtils.getInstance().getInt(Constant.SP_KEY_RESTAURANT_ID, -1, Constant.SP_ACCOUNT) - 1, -1, -1.0, -1, -1)
        swi_early_pay_discount_open.setOnCheckedChangeListener { buttonView, isChecked ->
            earlyPayDiscountRequest.isOpen = if (isChecked) 1 else 0
            et_early_pay_discount.isEnabled = isChecked
        }
        btn_early_pay_discount_save.setOnClickListener {
            earlyPayDiscountRequest.id = mEarlyPayDiscountEntity?.id!!
            earlyPayDiscountRequest.isShare = mEarlyPayDiscountEntity?.isShare!!
            earlyPayDiscountRequest.priority = mEarlyPayDiscountEntity?.priority!!
            earlyPayDiscountRequest.discount = et_early_pay_discount.removeHanziAndLetters().toDouble()
            mPresenter?.saveEarlyPayDiscount(earlyPayDiscountRequest)
        }
    }

    override fun getEarlyPayDiscountResult(isSuccess: Boolean, code: Int, msg: String?, t: EarlyPayDiscountEntity?) {
        if (isSuccess) {
            mEarlyPayDiscountEntity = t
            swi_early_pay_discount_open.isChecked = t?.isOpen == 1
            et_early_pay_discount.isEnabled = t?.isOpen == 1
            et_early_pay_discount.setText("${t?.discount}")
        } else {
            showErrorPage(View.OnClickListener { mPresenter?.getEarlyPayDiscount() }, msg!!)
        }
    }

    override fun saveEarlyPayDiscountResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            finishDelay()
        }
        toast(msg!!)
    }
}
